import wx
import sys
import os.path
import getopt

from utils import tracer
from gui.mdiFrame import MDIFrame
from gui.toolbarManager import ToolbarManager
from gui.menuManager import MenuManager
from gui.paneManager import PaneManager
from gui.statusbarManager import StatusbarManager
from plugins.pluginManager import PluginManager

class GlobalSettings(object):
  def __init__(self):
    self._homePath = ''
    self._workingPath = ''
    self._isDebugMode = False

  def getWorkingPath(self):
    return self._workingPath
  def setWorkingPath(self, path):
    if not os.path.exists(path):
      self._workingPath = ''
    else:
      self._workingPath = path

  def getHomePath(self):
    return self._homePath
  def setHomePath(self, path):
    if not os.path.exists(path):
      self._homePath = ''
    else:
      self._homePath = path

  def isDebugMode(self):
    return self._isDebugMode
  def setDebugMode(self, mode=True):
    self._isDebugMode = mode

class MainApp(wx.App):
  def __init__(self):
    wx.App.__init__(self)

    self._globalSettings = GlobalSettings()
    self._globalSettings.setDebugMode(False)
    self._globalSettings.setHomePath(os.path.realpath(os.path.dirname(__file__)))
    self._globalSettings.setWorkingPath(self._globalSettings.getHomePath())

  def initApp(self, argv):
    tracer.writelist(argv)

    try:
      opts, args = getopt.getopt(argv[1:], "hd:v", ["help", "debug=", 'home=', "workpath="])
    except getopt.GetoptError, err:
      tracer.writeln('Get opt failed.%s%s' % (os.linesep, str(err)))
      sys.exit(2)
  
    tracer.writeln('-- opt info -----------------------')
    tracer.writeln('count: %d' % len(opts))
    
    for opt, arg in opts:      
      if opt.lower() == '--debug' or opt.lower() == '--d':
        self.globalSettings().setDebugMode(arg.lower() == 'true')
      elif opt.lower() == '--workpath' or opt.lower() == '--w':
        self.globalSettings().setWorkingPath(os.path.abspath(arg))
      elif opt.lower() == '--home':
        self.globalSettings().setHomePath(os.path.abspath(arg))

    tracer.writeln('%s== Global Settings =========================' % os.linesep)
    tracer.writeln('Working Path: %s' % self.globalSettings().getWorkingPath())
    tracer.writeln('Home Path: %s' % self.globalSettings().getHomePath())
    tracer.writeln('Debug Mode: %s' % self.globalSettings().isDebugMode())
    tracer.writeln('=====================================%s' % os.linesep)

    frame = MDIFrame('wxPyDev v0.1.0.0')
    frame.SetSize((800,600))

    pluginManager = PluginManager()
    frame.setPluginManager(pluginManager)

    paneMgr = PaneManager('paneMgr')
    frame.addUIManager(paneMgr)

    statusBarManager = StatusbarManager('statusbarMgr')
    frame.addUIManager(statusBarManager)

    toolbarMgr = ToolbarManager('toolbarMgr')
    frame.addUIManager(toolbarMgr)

    menuMgr = MenuManager('menuMgr')
    frame.addUIManager(menuMgr)

    frame.Show(True)
    self.SetTopWindow(frame)

  def globalSettings(self):
    return self._globalSettings

  def start(self):
    self.MainLoop()

if __name__ == '__main__':
  app = MainApp()
  app.initApp(sys.argv)
  app.start()
